<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择结束时间" v-decorator="['finishTime', validatorRules.finishTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="state==12">
            <a-form-item label="中标人" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <j-dict-select-tag type="list" v-decorator="['type']" :trigger-change="true" dictCode="property_type"/>-->
              <a-select
                placeholder="请选择中标人"
                @change="handleChange"
                v-decorator="['customerId', validatorRules.customerId]"
              >
                <a-select-option :value="i.customerId" v-for="(i,index) in arr" >
                  {{i.contactsName}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :span="24" v-if="state==12">
            <a-form-item label="成交金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :min="0" :formatter="value => `${value}万元`" v-decorator="['transactionPrice', validatorRules.transactionPrice]" placeholder="请输入成交金额" style="width: 100%"/>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="结果描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea  placeholder="请输入结果描述" :rows="4" v-decorator="['desc', validatorRules.desc]"></a-textarea>
            </a-form-item>
          </a-col>


          <a-col :span="24" style="text-align: center">
            <a-button @click="submitForm" type="primary">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>

  </a-spin>
</template>

<script>

  import { httpAction, getAction ,putAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'
  import JAreaLinkagePca from '@/components/jeecg/JAreaLinkagePca'

  export default {
    name: 'ChengjiaoForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
      JEditor,
      JAreaLinkagePca
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        state:0,
        city:'',
        province:'',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          customerId: {
            rules: [
              { required: true, message: '请选择中标人!'},
            ]
          },
          finishTime: {
            rules: [
              { required: true, message: '请输入完成时间!'},
            ]
          },
          transactionPrice: {
            rules: [
              { required: true, message: '请输入成交金额!'},
              {
                validator: (rule, value, callback) => {
                  var reg = new RegExp("^[0-9]*$");
                  if (!reg.test(value)){
                    return false
                  }else {
                    return true
                  }
                  // return this.$u.test.mobile(value);
                },
                message: '成交金额请输入数字',
                // 触发器可以同时用blur和change
                trigger: ['change','blur'],
              }
            ]
          },
          desc: {
            rules: [
              { required: true, message: '请输入结果描述!'},
            ]
          },
          province: {
            rules: [
              { required: true, message: '请输入省!'},
            ]
          },
          city: {
            rules: [
              { required: true, message: '请输入市!'},
            ]
          },
          enrollStartTime: {
            rules: [
              { required: true, message: '请输入报名开始时间!'},
            ]
          },
          enrollEndTime: {
            rules: [
              { required: true, message: '请输入报名结束时间!'},
            ]
          },
        },
        url: {
          add: "/property/add",
          edit: "/property/edit",
          queryById: "/property/queryById",
          remark:'/property/propertyApprove',
          streamBeatAndDeal:"/property/streamBeatAndDeal"//成交或流拍
        },
        remarkStateStr:1,//审核 拒绝or同意 状态
        remark:'',  //意见
        details:true,
        realName:'',
        tel:'',
        arr:[]
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.arr = record.list
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          console.log(this.arr)
          this.form.setFieldsValue(pick(this.model,'finishTime','transactionPrice','customerId','desc'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;

            // let formData = Object.assign(this.model, values,{province:this.province,city:this.city});
            let formData1 = {
              customerId: values.customerId,
              desc: values.desc,
              finishTime: values.finishTime,
              id: this.model.id,
              state: this.state,
              transactionPrice: values.transactionPrice
            }
            console.log("表单提交数据",formData1)
            putAction(this.url.streamBeatAndDeal,formData1).then((res)=>{
              console.log(res)
              if(res.success){

                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'finishTime','transactionPrice','desc'))
      },

      //省市联动
      areaChange(e){
        // console.log(e)
        this.province = e[0]
        this.city = e[1]
      },

      //审批：同意/拒绝
      remarkState(e){
        this.remarkStateStr = e.target.value
      },



      handleChange(e){
        console.log(e)
      }
    }
  }
</script>
<style scoped>
  .box1{
    border: 1px solid #e1e1e1;
    border-radius: 5px;
    padding: 20px;
  }
</style>